package net.lgpage.util;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class ObjectRandom {

	String names[] = {"Longwide","二狗","小四","瓶瓶","叉叉"};
	String url[] = {"http://www.csdn.net/","http://search.jikexueyuan.com/course/?q=HTML5&page=6","https://www.vitamio.org/docs/Tutorial/2013/0508/15.html","http://www.w3school.com.cn/schema/schema_complex_anyattribute.asp","http://fanyi.baidu.com/#en/zh/prarm"};
	String tels[] = {"13546775200","15574366354","17760040729","13333333333","13111111111"};
	String titles[] = {"为什么选择Kubernetes作为云平台的微服务治理框架","新的函数式编程语言可以为应用生成C，Python代码"
			,"Node.js 6紧跟V8引擎更新，提高了速度和安全性"};
	
	String contents[] = {"很多同学在做技术选型时，往往过于关注技术/功能上的比较，陷入技术细节和功能特性上的争论。本文以“选择Kubernetes作为云平台的微服务治理框架”为例，介绍普元在做与云平台相关的开源技术选型时的一些方法和经验。"
			,"哥本哈根大学计算机科学系的研究员最近发布了函数式编程语言Futhark，它在GPU编程方面比标准的C/C++框架更加优秀，可以给已经存在的应用生成C和Python代码，并完美地整合进去。"
			,"Node.js是很热门的服务器端平台，基于JavaScript语言。昨天，Node.js发布了版本6，主要提高了速度和安全性，接受最新的JavaScript规范。"
	};
	String dates[] = {"2016-04-26","2015-08-11","2015-12-24","2016-02-10"};
	String times[] = {"2016-04-26 11:30","2015-08-11 12:30","2015-12-24 10:05","2016-02-10 14:22"};
	
	public <T>T randomObj(Class<T>clazz){
		T t = null;
		try {
			t = clazz.newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		Field fs[] = clazz.getDeclaredFields();
		for(int i = 0;i<fs.length;i++){
			fs[i].setAccessible(true);
			String fname = fs[i].getName();
			fname = fname.toLowerCase();
			Object ro = null;
			if(fs[i].getType()==String.class){
				if(fname.contains("name")){
					ro = random(names);
				}else if(fname.contains("title")){
					ro = random(titles);
				}else if(fname.contains("time")){
					ro = random(times);
				}else if(fname.contains("date")){
					ro = random(dates);
				}else if(fname.contains("content")){
					ro = random(contents);
				}else if(fname.contains("url")){
					ro = random(url);
				}
			}
			try {
				fs[i].set(t, ro);
			} catch (IllegalArgumentException e) {
			} catch (IllegalAccessException e) {
			}
		}
		return t;
	}
	private <T>T random(T[]objs){
		int rd = (int) Math.floor(Math.random()*objs.length);
		return objs[rd];
	}
	public int random15(){
		int rd = (int) Math.floor(Math.random()*15);
		return rd;
	}
	public static <T>List<T>randomList(Class<T>clazz){
		ObjectRandom ro = new ObjectRandom();
		List<T>list = new ArrayList<>();
		int rd = ro.random15();
		for(int i = 0;i<rd;i++){
			list.add(ro.randomObj(clazz));
		}
		return list;
	}
}
